package com.deuwise.job.customquartz;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.deuwise.common.utils.DateUtils;
import com.deuwise.common.utils.FileHelperUtils;
import com.deuwise.common.utils.SpringContextHolder;
import com.deuwise.job.customquartz.base.JobRun;
import com.deuwise.job.customquartz.base.JobRunLogHelper;
import com.deuwise.job.entity.JobRunLogDo;
import com.deuwise.job.entity.TaskDo;
import com.deuwise.job.service.TaskDoService;
import com.deuwise.system.entity.ConfigDo;
import com.deuwise.system.entity.LogDO;
import com.deuwise.system.service.ConfigDoService;
import com.deuwise.system.service.LogDoService;
import com.github.kevinsawicki.http.HttpRequest;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import javax.swing.*;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JobWarnRisk implements Job, JobRun {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public void run() {
        ApplicationContext applicationContext = SpringContextHolder.getApplicationContext();
        TaskDoService taskDoService=applicationContext.getBean(TaskDoService.class);
         Wrapper<TaskDo> wrapper=new EntityWrapper<>();
         wrapper.eq("method_name","com.deuwise.job.customquartz.JobWarnRisk");
        TaskDo taskDo=taskDoService.selectOne(wrapper);
        if(taskDo!=null) {
            Pattern pattern = Pattern.compile("http://[\\S\\.]+[:\\d]?[/\\S]+\\??[\\S=\\S&?]+[^\u4e00-\u9fa5]");
            Matcher matcher = pattern.matcher(taskDo.getDescription());
            StringBuffer buffer = new StringBuffer();
            while (matcher.find()) {
                buffer.append(matcher.group());
                buffer.append("\r\n");
            }
            System.out.println(buffer.toString());
            if (matcher.find()){
                System.out.println(matcher.group());
            }
            HttpRequest httpRequest = HttpRequest.get(buffer.toString(),null, false);

        }

         //taskDoService.selectOne(wrapper);
        System.out.println("JobWarnRisk定时获取高危预警信息：执行完毕=======================");
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //添加执行日志
        System.out.println("JobWarnRisk定时获取高危预警信息日志：启动任务=======================");
        /*
         * 添加运行日志
         *
         * */
        JobRunLogHelper jobRunLogHelper= SpringContextHolder.getBean(JobRunLogHelper.class);
        JobRunLogDo jobRunLogDo=new JobRunLogDo();
        jobRunLogHelper.getBaseRunLog(context,jobRunLogDo, this.getClass());
    }
}
